<template>
	<view class="contain">
		<!-- 课程列表 -->
		<view class="courses" v-if="isOpen">
			<view class="clist" v-for="(item,index) in coursesList.data" @click="goPlay(item.id)">
				<view class="videophoto"><image src="https://yulistudio.oss-cn-shenzhen.aliyuncs.com/images/icons/video.png" mode="aspectFit"></image></view>
				<view class="vdesc">
					<text class="mid blod line-clamp-2">{{item.coursename}}</text>
					<text class="mt-1 mb-1"><text class="blod">{{item.courselev}}</text> {{item.totalcourses}}节课 / {{item.coursetime}}分钟</text>
					<text class="small">{{item.weekname}} {{replaceStr(item.classtime)}} </text>
				</view>
				<view class="videoOptions">
					<view><text class="red">￥{{item.courseprice}}</text></view>
					<view class="mt-1"><u-button type="primary" text="试看" size="small" shape="circle"></u-button></view>
				</view>
			</view>
		</view>
		<view v-else>
			<view style="margin-top:60rpx;"><u-empty mode="data" text="暂无收藏"></u-empty></view>
		</view>
	</view>
</template> 

<script setup lang='ts'>
import { ref,reactive,watchEffect } from 'vue'
import {  onLoad,onShow,onReachBottom } from '@dcloudio/uni-app'
import { bus } from '@/utils/bus'
import { userStore } from '@/stores/userStore'
let store = userStore();	
let { hasLogin,userData } = userStore();

let pageNum=ref(1);
let pageSize=ref(5);
let totalData=ref(0);


//是否打开
let isOpen=ref(false);

let timer=null;
let isLoaded=ref(false);
let coursesList=reactive<any>({data:[]});		
	

//读取开关
const getOpenCourse=()=>{
	let paraData={}
	console.log("读取开关参数:",paraData);
	uni.$tools.Reqeust('/api/getOpenCourse',paraData,'POST').then((res:any)=>{
		console.log('读取开关参数:==>',res);
		if (res.data.code == 200) {
			isOpen.value=res.data.data[0].switchtype===1?true:false;
		}
	})	
}	
	

const replaceStr=(inputWord:string)=>{
	return inputWord.replace("至","--");
}

const goPlay=(vid:number)=>{
	//console.log("视频id"+vid.toString());
	uni.setStorageSync("videoId",vid)
	uni.$tools.GoUrl('/pages/video/video');
}

const getMyFavs=()=>{
	uni.showLoading({
		title:"加载中..."
	})
	let paraData={
		userid:userData.userinfo.id
	}
	console.log("课程参数:",paraData);
	uni.$tools.Reqeust('/api/getMyFavsList',paraData,'POST').then((res:any)=>{
		console.log('课程返回:==>',res);
		if (res.data.code == 200) {
			uni.hideLoading();
			coursesList.data=res.data.data;
			isLoaded.value=true;
		}else{
			uni.hideLoading();
		}
	})	
}


onLoad(()=>{
	//读取课程信息
	readFavs();
});


onShow(()=>{
	getOpenCourse();
	if(coursesList.data.length<=0){
		if(isLoaded.value){
			if(timer) clearTimeout(timer);
			return;
		}
		timer=setTimeout(()=>{
			readFavs(); //自刷新一次
		},1200);			
	}
	console.log("111",coursesList.data.length);
});


const readFavs=()=>{
	console.log("是否已登录",hasLogin);
	if(uni.getStorageSync("hasLogin")!=""){
		hasLogin=true;
		uni.removeStorageSync("hasLogin");
	}
	if(hasLogin){
		getMyFavs();		
	}else{
		uni.$tools.Toast("请登录后访问");
		setTimeout(()=>{
			store.$state.backRouter="/pages/myfav/myfav";
			uni.$tools.GoUrl('/pages/login/login');
		},1000);
	}	
}

onReachBottom(()=>{
	console.log("到底了...加载数据");
})
	
</script>
<style>
	page{
		background:#F1F1F1;
	}
</style>
<style lang='scss' scoped>
	@import "myfav.scss";
</style>